/* UVa- 10189
 * Author- Ajeet Singh
 * http://uva.onlinejudge.org
 * id- ajeet.singh82
 * Contact- ajeet.singh82@gmail.com
 *
 * 
 * Algorithm: 
 *
 *
 * Build:
 * ...
 */
#include <iostream>
#include <iterator> 
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <locale>
#include <queue>
#include <stack>
using namespace std;

typedef pair<unsigned short, unsigned short> point;
unsigned short M = 0, N = 0;
vector<string> dc;

inline unsigned short  Max(unsigned short x, unsigned short y) {
	return x ^ ((x ^ y) & -(x < y));
}

inline unsigned short Min(unsigned short x, unsigned short y) {
	return y ^ ((x ^ y) & -(x < y));
}


inline char test(const point& T) {
	return dc[T.second][T.first];
}

void process_I(unsigned short M, unsigned short N){
	dc.clear();
	string pixelrow(M, 'O');
	while(N-- > 0) dc.push_back(pixelrow);
}

void apply_color(point& A, point& B, char C) {
	if (A == B) {
		dc[A.second][A.first] = C;
	} else if (A.second == B.second) {
		while(A.first <= B.first) {
			dc[A.second][A.first++] = C;
		}
	} else if (A.first == B.first) {
		while(A.second <= B.second) {
			dc[A.second++][A.first] = C;
		}
	
	} else {
		unsigned short y =0;
		while(A.second+y <= B.second) {
			unsigned short x =0;
			while(A.first+x <= B.first) {
				dc[A.second+y][A.first+x] = C;
				x++;
			}
			y++;
		}
	
	}
}

void fill_color(const point& A, char P, char C) {
	queue<point> Q;
	stack<point> S;
	point node;
	Q.push(A);
	while(Q.empty() == false) {
		node = Q.front();
		dc[node.second][node.first] = C;
		S.push(node);
		unsigned short i=1;
		while(node.first+i < M && test(point(node.first+i, node.second)) == P) {
			dc[node.second][node.first+i] = C;
			S.push(point(node.first+i, node.second));
			i++;
		}
		i = 1;
		while((unsigned short) (node.first-i) < M && test(point(node.first-i, node.second)) == P) {
			dc[node.second][node.first-i] = C;
			S.push(point(node.first-i, node.second));
			i++;
		}
		while(S.empty() == false) {
			node = S.top();
			if (node.second+1 < N && test(point(node.first, node.second+1)) == P) 
				Q.push(point(node.first, node.second+1));
			if ((unsigned short) (node.second-1) < N && test(point(node.first, node.second-1)) == P)
				Q.push(point(node.first, node.second-1));
			S.pop();
		}
		Q.pop();
	}
}

bool process_cmd(const string& cmdline) {
	bool result = false;
	stringstream tokens(stringstream::in | stringstream::out);
	tokens << cmdline;
 	char cmd;
	tokens >> cmd;
	switch(cmd) {
		case 'I':
			tokens >> M >> N;
			if(M > 0 && M <= 250 && N> 0 && N <= 250)
				process_I(M, N);
			else 
				M=N=0;
			break;
		case 'C':
			if(M > 0 && M <= 250 && N> 0 && N <= 250)
				process_I(M, N);
			break;
		case 'L':
			{
				unsigned short X, Y;
				char C;
				tokens >> X >> Y >> C;
				X--; Y--;
				if(C < 'A' || C > 'Z')
					break;
				if(X >= 0 && X < M && Y >= 0 && Y < N) {
					point A(X, Y);
					apply_color(A, A, C);
				}
			}
			break;
		case 'V':
			{
				unsigned short X, Y1, Y2;
				char C;
				tokens >> X >> Y1 >> Y2 >> C;
				X--; Y1--; Y2--;
				if(C < 'A' || C > 'Z')
					break;
				if(X >= 0 && X < M && Y1 >= 0 && Y1 < N && Y2 >=0 && Y2 < N) {
					point A(X, min(Y1, Y2)), B(X, max(Y1, Y2));
					apply_color(A, B, C);
				}
			}
			break;
		case 'H':
			{
				unsigned short X1, X2, Y;
				char C;
				tokens >> X1 >> X2 >> Y >> C;
				X1--; X2--; Y--;
				if(C < 'A' || C > 'Z')
					break;
				if(X1 >= 0 && X1 < M && X2 >= 0 && X2 < M && Y >=0 && Y < N) {
					point A(min(X1, X2), Y), B(max(X1, X2), Y);
					apply_color(A, B, C);
				}
			}
			break;
		case 'K':
			{
				unsigned short X1, X2, Y1, Y2;
				char C;
				tokens >> X1 >> Y1 >> X2 >> Y2 >> C;
				X1--; X2--; Y1--; Y2--;
				if(C < 'A' || C > 'Z')
					break;
				if(X1 >= 0 && X1 < M && X2 >= 0 && X2 < M && Y1 >=0 && Y1 < N && Y2 >=0 && Y2 < N) {
					if(X1 > X2) swap(X1, X2);
					if(Y1 > Y2) swap (Y1, Y2);
					point A(X1, Y1), B(X2, Y2);
					apply_color(A, B, C);
					
				}
			}
			break;
		case 'F':
			{
				unsigned short X, Y;
				char C;
				tokens >> X >> Y >> C;
				X--; Y--;
				if(C < 'A' || C > 'Z')
					break;
				if(X >= 0 && X < M && Y >=0 && Y < N) {
					point A(X, Y);
					if(test(A) != C) {
						fill_color(A, test(A), C);
					}
				}
			}
			break;
		case 'S':
			{	
				string filename;
				tokens >> filename;
				cout << filename << endl;
				copy(dc.begin(), dc.end(), ostream_iterator<string>(cout, "\n"));
			}
			break;
		case 'X':
			result= true;
			break;
		default:
			break;
	}
	return result;
}

/* main
 * */
int main() {
	bool exit = false;
	while ( exit == false ) {
		string cmdline;
		getline(cin, cmdline);
		exit = process_cmd(cmdline);
	}
	return 0;
}
